"use strict";

toastr.options = {
    closeButton: false,
    debug: false,
    progressBar: true,
    positionClass: "toast-top-center",
    onclick: null,
    showDuration: "300",
    hideDuration: "1000",
    timeOut: "2000",
    extendedTimeOut: "1000",
    showEasing: "swing",
    hideEasing: "linear",
    showMethod: "fadeIn",
    hideMethod: "fadeOut"
};
var favorite = void 0,
    page = 1,
    pageSize = 10,
    free_Disabled = false;
/*
**
* 页面初始化时加载 window.onload
*/
function geeurlcodeObj() {
    //获取url上面的所有参数 返回一个 对象
    try {
        var urlcan = decodeURIComponent(window.location.search);;
        urlcan = urlcan.split("?")[1]; //数组
        urlcan = urlcan.replace(/\s/g, "");
        //去空格
        var arr = urlcan.split("&"); //数组
        var obj = {};
        for (var i = 0; i < arr.length; i++) {
            var arritem = arr[i].split("=");
            if (arritem[1]) {
                obj[arritem[0]] = arritem[1];
            } else {
                obj[arritem[0]] = "";
            }
        }

        return obj;
    } catch (e) {
        console.log(e);
        return {}; //错误后返回空对象
    }
}

//收藏 取消收藏
function colletThis() {
    if (validLogin()) {
        var params = {
            bizType: 2,
            bizId: geeurlcodeObj().id
        };
        if (favorite == 0) {
            //收藏
            $.server(params, '/iagent/v1/addFavorite').then(function (res) {
                toastr.info('收藏成功');
                getDetail(geeurlcodeObj().id);
            });
        } else {
            //取消收藏
            $.server(params, '/iagent/v1/delFavorite').then(function (res) {
                toastr.info('取消收藏成功');
                getDetail(geeurlcodeObj().id);
            });
        }
    }
}

function getDetail(id) {
    //经过调用接口拿到详情
    $.server({ courseId: id }, '/website/v1/courseDetail').then(function (res) {
        //模拟详情数据
        var detail = res.result;
        $("#lessonImg").attr("src", detail.poster);
        if (detail.priceStr == 0) {
            $("#price").text("免费");
        } else {
            $("#price").text("￥" + detail.priceStr);
        }

        $(".lessonTitle").text(detail.courseName);
        $(".buyCount").text(detail.buyCount + "人购买");
        $(".lessonIntrduce").html(detail.detail);
        $(".goodRate").text('好评率：' + (detail.rate ? detail.rate : "暂无评论"));
        $(".commentCount").text("(" + detail.commentCount + ")");

        favorite = detail.favorite;
        if (detail.favorite == 0) {
            $(".defult_img").show();
            $(".active_img").hide();
        } else {
            $(".defult_img").hide();
            $(".active_img").show();
        }
        if (Number(detail.priceStr) > 0) {
            $('.buyIt').show();
            $('.freeSign').hide();
        } else {
            //免费
            if (detail.buy == 1) {
                $(".freeSign").text("已报名");
                $(".freeSign").addClass("free_Disabled");
                free_Disabled = true;
            }
            $('.buyIt').hide();
            $('.freeSign').show();
        }
        var html = '',
            length = detail.catalogs.length,
            catalogueList = detail.catalogs;

        for (var i = 0; i < length; i++) {
            var imghtml = "<img src=\"images/train/play.png\" alt=\"\" class=\"sectionIcon\">\n            <img src=\"images/train/paly_1.png\" alt=\"\" class=\"sectionIcon sectionIconActive\">";
            if (catalogueList[i].catalogType == 1) {
                //直播
                imghtml = "<img src=\"images/train/zb_defult.png\" alt=\"\" class=\"sectionIcon\">\n                <img src=\"images/train/zb_active.png\" alt=\"\" class=\"sectionIcon sectionIconActive\">";
            } else if (catalogueList[i].catalogType == 3) {
                //资料
                imghtml = "<img src=\"images/train/zl_defult.png\" alt=\"\" class=\"sectionIcon\">\n                <img src=\"images/train/zl_active.png\" alt=\"\" class=\"sectionIcon sectionIconActive\">";
            }
            html += "<div class=\"chapterSection flexLay\">\n                <div class=\"sectionleft comflex\">\n                " + imghtml + "\n                <span>" + catalogueList[i].titlle + "\uFF08" + (catalogueList[i].catalogType == 1 ? "直播" : catalogueList[i].catalogType == 2 ? '视频' : '资料') + "\uFF09</span>\n                </div>\n                <div class = \"startStudy\" onclick = \"startStudy(" + catalogueList[i].catalogId + "," + catalogueList[i].catalogType + ",'" + catalogueList[i].titlle + "')\"> \u5F00\u59CB\u5B66\u4E60 </div>\n            </div>";
        }
        $("#catalogueBox").html(html);

        $(".chapterSection").click(function (e) {
            $(".chapterSection").removeClass("chapterFox");
            $(this).addClass("chapterFox");
        });
    });
}
//获取评论列表
function getCommentList(id) {
    $.server({ bizId: id, bizType: 2, pageNum: page, pageSize: pageSize }, '/iagent/v1/queryBizCommentList').then(function (res) {
        var obj = res.result.data;
        var html = '';
        for (var i = 0; i < obj.length; i++) {
            html += "<div class=\"flexLay commentBox\">\n                <img src=\"" + obj[i].avatar + "\" class=\"avatar\" alt=\"\">\n                <div class=\"content\">\n                    <div class=\"userName\">" + obj[i].userName + "</div>\n                    <div class=\"timer\">" + obj[i].createTime + "</div>\n                    <div class=\"descipt\">" + obj[i].comment + "</div>\n                </div>\n            </div>";
        }
        $("#commentList").append(html);
        if (res.result.totalPage <= page) {
            $(".viewMore").fadeOut();
            $(".nocontent").fadeIn();
        } else {
            $(".viewMore").fadeIn();
            $(".nocontent").fadeOut();
        }
    });
}

//免费报名
function freeSign() {
    if (free_Disabled) {
        return;
    }
    $.server({ courseId: geeurlcodeObj().id }, '/iagent/v1/courseBuy').then(function (res) {
        toastr.info('恭喜你,报名成功，现在可以进入目录开始学习了');
        getDetail(geeurlcodeObj().id);
    });
}
//开始学习
function startStudy(catalogId, catalogType, titlle) {
    $.server({ "courseId": geeurlcodeObj().id, "catalogId": catalogId }, '/iagent/v1/courseAuth').then(function (res) {
        console.log(res, 898)
        if (res.result.catalogType == 3) {
            //文档
            var a = document.createElement('a');
            a.href = res.result.url;
            // a.download='test.word';
            a.click();
            toastr.info('下载成功');
        } else if (res.result.catalogType == 2) {
            //视屏
            sessionStorage.catalog_Vedio = res.result.url;
            sessionStorage.catalog_Title = titlle;
            window.location.href="video.html"
            // window.open(res.result.url);
        } else if (res.result.catalogType == 1) {
            //直播
            sessionStorage.catalog_Vedio = res.result.url;
            sessionStorage.catalog_Title = titlle;
            window.open("player.html");
        }
    });
}
//查看更多
function viewMore() {
    page++;
    this.getCommentList(geeurlcodeObj().id);
}
$(function () {
    //拿到上一个页面给过来的参数
    var options = geeurlcodeObj();
    getDetail(options.id);
    getCommentList(options.id);
    if (options.route == 1) {
        $(".way_title").text("首页");
    } else if (options.route == 1) {
        $(".way_title").text("专家详情");
    }
    if (options.index == 1) {
        $(".babelTitle").text("专家课程");
    } else if (options.index == 2) {
        $(".babelTitle").text("保险讲堂");
    } else if (options.index == 3) {
        $(".babelTitle").text("行业知识");
    } else if (options.index == 4) {
        $(".babelTitle").text("直播课堂");
    }
    //栏目的title点击
    $(".lanmuBar>.lmTitle").click(function (e) {
        var index = e.currentTarget.dataset.index;
        if ($(this).hasClass('active')) {
            return;
        }
        $(".lanmuBar>.lmTitle").removeClass("active");
        $(this).addClass("active");
        if (index == 1) {
            //课程概述
            $(".lessonIntrduce").fadeIn();
            $(".catalogueBox").fadeOut();
            $(".commentListBox").fadeOut();
        } else if (index == 2) {
            //目录
            $(".lessonIntrduce").fadeOut();
            $(".catalogueBox").fadeIn();
            $(".commentListBox").fadeOut();
        } else if (index == 3) {
            //
            $(".commentListBox").fadeIn();
            $(".lessonIntrduce").fadeOut();
            $(".catalogueBox").fadeOut();
        }
    });
});